<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Cate;
use app\common\model\UserToken;

class Navigation extends Api {
    public function reg()
    {
        
        $un = new \app\common\model\Navigation();
        $data =[];
        if(empty(input('uid'))){
            return  showErr('请登录');
        }
        if(empty(input('web_url'))){
            return  showErr('请填写网址');
        }
        if(empty(input('web_name'))){
            return  showErr('请填写网址名字');
        }
        if(empty(input('cate_name'))){
            return  showErr('请填写网址名字');
        }
        $data['web_icon_url'] =input('web_icon_url');
        $data['table'] =input('table');
        $data['uid'] =input('uid');
        $data['web_url'] =input('web_url');
        $data['web_name'] =input('web_name');
        //查询分类是不是存在不存在添加并返回id
        $id = $this->addCategory($data['uid'],input('cate_name'));
        $data['cate_name'] =$id;
        $result = $un->reg($data);
        if($result){
            $info['code'] ='1';
            $info['msg'] ='收藏成功';
            return showOk($info,'收藏成功');


        }else{
            return showErr('收藏失败');
        }
    }
    /*
 * 获取用户分类
 */
    public function getCategory(){
        $un = new \app\common\model\Cate();
        $data =[];
        $data['uid'] = input('uid');
        if(empty(input('uid'))){
            return showErr('缺失参数uid');
        }
        $cinfo = $un->where('uid',input('uid'))->select()->toArray();
        if(empty($cinfo)){
            return showErr('添加分类');
        }
        if($cinfo){
            $info['code'] ='1';
            $info['msg'] ='查询成功';
            $info['data'] =$cinfo;
            return showOk($info,'查询成功');
        }else{
            return showErr('查询失败');
        }
    }
    /**
     * 添加用户分类
     */
    public function addCategory($uid,$cate_name){
        $un = new \app\common\model\Cate();
        $cinfo = $un->where('uid',$uid)->where('id',$cate_name)->find();
        if(empty($cinfo)){
            $data['uid'] = $uid;
            $data['cate_name'] = $cate_name;
            $id = $un->insertGetId($data);
        }else{
            $id = $cinfo['id'];
        }
        return $id;
    }

}